package server.config.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.springframework.http.HttpStatus;
import server.service.RedisService;
import server.tool.tool.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SystemLogoutFilter extends LogoutFilter {
    private RedisService redisService;

    public SystemLogoutFilter(RedisService redisService) {
        this.redisService = redisService;
    }

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse res) throws Exception {
        HttpServletResponse response = (HttpServletResponse) res;
        HttpServletRequest QErequest = (HttpServletRequest) request;
        response.setHeader("Access-control-Allow-Origin", QErequest.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,token");
        response.setHeader("X-Requested-With", QErequest.getHeader("X-Requested-With"));
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        response.setStatus(HttpStatus.OK.value());
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        if (WebUtils.isAjaxRequest(QErequest)) {
            return false;
        }
        return super.preHandle(request, response);
    }
}
